import { Widget, _decorator, Node, Label, Toggle, ScrollView, Prefab, instantiate } from 'cc';
import { UIBase } from '../scriptBase/UIBase';
import { UI } from '../enum/UI';
import { CfgLoginReward } from '../config/CfgLoginReward';
import { ArrayUtil } from '../util/ArrayUtil';
import { StorageUtil } from '../util/StorageUtil';
import { ShopItem } from '../uiItem/ShopItem';
import { GameUtil, shopData } from '../util/GameUtil';
import { GameMgr } from '../manager/GameMgr';
import { cfg_level_gs } from '../config/cfg_level';
import { DtoLevelData } from '../dto/DtoLevelData';
const { ccclass, property, requireComponent } = _decorator;

//素材界面
@ccclass('UI/UIShopView')
@requireComponent(Widget)
export class UIShopView extends UIBase {

    @property(Node)
    public itemContent: Node = null;

    private info:any[];

    public onOpen(data?: any): void {
        this.refresh();
    }

    public onClose(data?: any): void {

    }

    protected onBtnCloseClick(): void {
        this.close()
    }

    public refresh(){
        this.info = cfg_level_gs;

        if (!this.info){
            return;
        }
        for (let index = 0; index < this.itemContent.children.length; index++) {
            let itemshow = this.itemContent.children[index];
            itemshow.active = false;
        }

        for (let index = 0; index < this.info.length; index++) {
            let itemData:DtoLevelData = this.info[index];
            let item;
            if (this.itemContent.children && this.itemContent.children[index]){
                item = this.itemContent.children[index];
            }else{
                item = instantiate(this.itemContent.children[0]);
                this.itemContent.addChild(item);
            }
            item.active = true;
            item.getComponent(ShopItem).setData(itemData);    
        }
    }

    protected onBtnGetAllClick(): void {
        this.close()
        this.open(UI.GetReward, CfgLoginReward)
        StorageUtil.setItem('loginRewardGetTime', Date.now())
    }

    protected onBtnGetOneClick(): void {
        this.close()
        this.open(UI.GetReward, [ArrayUtil.pickItem(CfgLoginReward)])
        StorageUtil.setItem('loginRewardGetTime', Date.now())
    }

}


